//
//  ListView.swift
//  swifttest1
//
//  Created by kologe on 2024/9/17.
//

import SwiftUI

struct ListView: View {
    @Binding var dataSource:Array<Scan>
    @Binding var isButtonTapped:Bool
    @State private var showMessageView = false
    @State private var showFileView = false
    
    var body: some View {
        VStack{
            HStack{
                Button(action: {
                    isButtonTapped.toggle()
                }, label: {
                    Text("return")
                })
                .nomalStyle()
                
                Spacer()
                
                Button(action: {
                    terminal_list.removeAllObjects()//清除所有的数据
                    Scan.sendData()
                }, label: {
                    Image("refresh", label: Text("refresh"))
                    
                })
                .nomalStyle()
                
            }
            ScrollView {
                let total:CGFloat = 4+2+4
                let percent1 = 4/total
                let percent2 = 2/total
                let percent3 = 4/total
                
                GeometryReader{ geometry in
                    HStack {
                        
                        Text("hostName").frame(width: geometry.size.width * percent1, alignment: .center).font(.headline)
                        Text("terminal_type").frame(width: geometry.size.width * percent2, alignment: .center).font(.headline)
                        Text("ip").frame(width: geometry.size.width * percent3, alignment: .center).font(.headline)
                        
                    }
                }
               
                ForEach(0..<dataSource.count, id: \.self) { row in
                    let temp = dataSource[row]
                    
                    
                    GeometryReader{ geometry in
                        HStack {
                            Button(action: {
                                clickTableLine(scan: temp)
                            }, label: {
                                
                                Text("\(temp.hostName)").frame(width: geometry.size.width * percent1, alignment: .center)
                                Text("\(temp.terminalType)").frame(width: geometry.size.width * percent2, alignment: .center)
                                Text("\(temp.ip)").frame(width: geometry.size.width * percent3, alignment: .center)
                            })
                            .frame(height: 20)
                            .background(
                                RoundedRectangle(cornerRadius: 3)
                                    .foregroundColor(.gray)
                            )
                            .buttonStyle(.plain)
                            .contextMenu {
                                // 定义菜单选项
                                Button("发送信息") {
                                    print("发送信息：\(temp.ip)")
                                    showMessageView = true
                                }
                                
                                Button("发送文件") {
                                    print("Option 2 selected for Item ")
                                    showFileView = true
                                    
                                }
                            }
                            .sheet(isPresented: $showMessageView){
                                MessageView(remoteHost: temp.ip,isShow: $showMessageView)
                            }
                            .sheet(isPresented: $showFileView){
                                FileMessageView(remoteHost:temp.ip,isShow: $showFileView)
                            }
                            
                        }
                    }
                }
            }.border(Color.black,width: 1)
            
            
        }
        
    }
    func clickTableLine(scan:Scan){
        
    }
    
}

//#Preview {
//    ListView(dataSource: terminal_list.array, isButtonTapped:.constant(false))
//}
